<template>
    <div class="admin-login">
        <div class="board">
            <div class="logo">管理后台</div>
            <el-form class="form" @submit.native.prevent>
                <el-form-item>
                    <el-input placeholder="账号" clearable="" v-model="form.account"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-input placeholder="密码" type="password" clearable="" v-model="form.password"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-switch :inactive-value="0" :active-value="1" v-model="remember"></el-switch>
                    <span class="remark">记住我, 下次自动登录</span>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :style="{width: '100%'}" native-type="submit" @click="submit">立即登录</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import lodash from "lodash";

export default {
    data() {
        return {
            form: {
                account: this.$cookie.get("account") || "",
                password: "",
            },
            remember: 1,
        };
    },
    methods: {
        clearStorage() {
            window.localStorage.clear();
            window.sessionStorage.clear();
        },
        submit() {
            const { remember } = this;
            const { localStorage, sessionStorage } = window;
            const tokenName = process.env.VUE_APP_TOKEN_KEY;

            const form = lodash.cloneDeep(this.form);

            if (remember) this.$cookie.set("account", form.account);

            this.$post("/user/login", form).then((res) => {
                const { code, data: token } = res;
                if (res.code == 200) {
                    this.clearStorage();
                    if (remember == 1) {
                        localStorage.setItem(tokenName, token);
                    } else {
                        sessionStorage.setItem(tokenName, token);
                    }
                    this.$router.push({ name: "console" });
                }
            });
        },
    },
};
</script>
<style lang="scss" scoped>
.admin-login {
    height: 100vh;
    width: 100vw;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #f6f6f6;
    background-image: url("../../assets/imgs/default-bg.jpg");
    background-size: cover;
    background-position: bottom center;

    .remark {
        font-size: 13px;
        color: gray;
        margin: 0px 10px;
    }

    .board {
        width: 360px;
        padding: 20px;
        background-color: #fff;
        border-radius: 10px;
        box-shadow: 0px 1px 5px rgba($color: #000000, $alpha: 0.1);

        .logo {
            display: flex;
            justify-content: center;
            align-items: center;
            font-size: 40px;
            font-weight: bold;
        }
        .form {
            padding: 30px 0px;
        }
    }
}
</style>